//加载网络图片
import 'package:flutter/material.dart';

import '../widgets/CommonHeader.dart';

Widget loadNetworkImage(String imageUrl,
    {double imgWidth = 150, double imgHeight = 150}) {
  Widget content = Image.network(imageUrl, fit: BoxFit.cover);
  content = SizedBox(
    width: imgWidth,
    height: imgHeight,
    child: content,
  );
  return content;
}

//加载本地图片
Widget loadLocalImage(String imageUrl) {
  Widget body = Image.asset(imageUrl, fit: BoxFit.cover);
  body = SizedBox(
    width: 150,
    height: 150,
    child: body,
  );
  return body;
}

//标题描述
Widget desc(String text) {
  Widget content =
      Text(text, style: const TextStyle(color: Colors.white, fontSize: 20));
  content = Center(child: content);
  content = Container(
    width: double.infinity,
    height: 40,
    decoration: const BoxDecoration(
      color: Colors.blueGrey,
    ),
    child: content,
  );

  return content;
}

//纵向间隔
Widget verticalSpacing(double height) {
  Widget content = SizedBox(height: height);
  return content;
}

//纵向间隔
Widget horizontalSpacing(double width) {
  Widget content = SizedBox(width: width);
  return content;
}

//简易页面部件
Widget commonScaffold(String pageName, List<Widget> children) {
  Widget body = Scaffold(
      appBar: CommonHeader(title: pageName), body: commonColumn(children));
  return body;
}

//body组件
Widget commonColumn(List<Widget> children) {
  Widget body = Column(
    mainAxisAlignment: MainAxisAlignment.center,
    spacing: 10,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: children,
  );
  body = SizedBox.expand(child: body);
  return body;
}

//默认居中row
Widget commonRow(List<Widget> children) {
  Widget body = Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: children,
  );
  return body;
}

//自定义Row
Widget customRow(double width, double height, List<Widget> children,
    {Color bgColor = Colors.white,
    double borderRadius = 0,
    bool isCrossCenter = true,
    double borderWidth = 0}) {
  Widget body = Row(
    mainAxisAlignment:
        isCrossCenter ? MainAxisAlignment.center : MainAxisAlignment.start,
    children: children,
  );
  body = Container(
    width: width,
    height: height,
    decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(borderRadius),
        border: Border.all(color: Colors.grey, width: borderWidth)),
    child: body,
  );
  return body;
}

//行文本
Widget rowText(String text) {
  Widget body = Text(
    text,
    style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
  );

  body = Center(child: body);
  body = Container(
      width: double.infinity,
      height: 40,
      margin: const EdgeInsets.only(top: 10),
      color: Colors.blueGrey,
      child: body);

  return body;
}

//标题文本 字体20 加粗
Widget commonTitle(String title,
    {bool isBold = true, double? fontSize, Color color = Colors.black}) {
  Widget body = Text(
    title,
    style: TextStyle(
      fontSize: fontSize ?? 20,
      fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
      color: color,
    ),
  );
  return body;
}

PreferredSizeWidget commonAppBar(String title) {
  Widget body = AppBar(
    title: commonTitle(title, color: Colors.white),
    centerTitle: true,
    backgroundColor: Colors.blueGrey,
  );
  PreferredSizeWidget content = PreferredSize(
    preferredSize: const Size.fromHeight(40),
    child: body,
  );
  return content;
}

Widget commonText(String text,
    {Color fontColor = Colors.black,
    double fontSize = 16,
    bool isBold = false}) {
  Widget body = Text(text,
      style: TextStyle(
          fontSize: fontSize,
          color: fontColor,
          fontWeight: isBold ? FontWeight.bold : FontWeight.normal));
  return body;
}

//功能按钮
Widget commonButton(String buttonName, Function callback) {
  Widget body = ElevatedButton(
      onPressed: () {
        callback();
      },
      child: commonText(buttonName));
  return body;
}

//输出函数
void printLog(String log) {
  debugPrint(log);
}
